package com.example.service;

import com.example.entity.Comment;
import com.example.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ApiCommentService {
    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private RedisTemplate redisTemplate;

    public Comment findById(Integer id){
        Object object =redisTemplate.opsForValue().get("api"+id);
        if (object!=null){
            return (Comment)object;
        }else {
            Optional<Comment> comment = commentRepository.findById(id);
            if (comment.isPresent()){
                redisTemplate.opsForValue().set("api"+id,comment.get());
                return comment.get();
            }
            return null;
        }
    }
}
